package com.baozh.api;


import com.alibaba.fastjson.JSON;
import com.baozh.resource.Series;
import com.baozh.resource.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Date;


/**
 * @author hubert
 * @date 2018/9/17
 * @description ApiController
 */
@Path("/info")
@Api(value = "基本信息接口")
public class ApiService {

	private final static Logger logger = LoggerFactory.getLogger(ApiService.class);


	@GET
	@Path("test")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "根据姓名查询基本信息", response = UserInfo.class)
	public UserInfo getUser(@ApiParam(value = "姓名") @QueryParam("name") String name) {
		logger.info("Jersey+swagger实现接口");
		UserInfo userInfo = new UserInfo();
		userInfo.setName(name);
		userInfo.setBirthDay(new Date());
		userInfo.setMobileNum("130XXXXXXXXX");
		return userInfo;
	}

	@GET
	@Path("message")
	public String getMessage(){
		return "success";
	}

	@GET
	@Path("series")
	@Produces(MediaType.APPLICATION_JSON)
	public Series getSeise() {
		Series series = new Series();
		series.setName("海王");
		series.setActor("亚特斯兰迪");
		series.setTags("DC，漫威，对抗");
		series.setStatus(1);
		return series;
	}

	/**
	 *  把字符串按json格式传，然后再转换为对象
	 * @param requestData {name: "复仇者联盟",actor: "钢铁侠"}
	 * @return object
	 * 从网络资料查询
	 * JSON序列化(Object => JSON),fackson最快
	 * JSON反序列化(JSON => Object),FastJSON稍快，但与Jackson相差不大
	 */
	@POST
	@Path("str")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Series getContentInfo(String requestData) {
		logger.info(requestData);
		Series series = JSON.parseObject(requestData,Series.class);
		return series;
	}

	/**
	 *  把对象当参数，key-value均需要用双引号
	 * @param series {"name":"复仇者联盟","actor":"钢铁侠"}
	 * @return
	 */
	@POST
	@Path("obj")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Series getContentInfo(Series series) {
		logger.info(series.getName());
		return series;
	}
}
